package org.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.common.PageData;
import org.example.common.PageQuery;
import org.example.common.ResponseData;
import org.example.domain.UpdateInfo;
import org.example.domain.UserInfo;
import org.example.domain.UserQuery;
import org.example.mapper.UserMapper;
import org.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public  class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;
@Override
    public ResponseData<String> enRoll(UserInfo userInfo){
    if (StrUtil.isBlank(userInfo.getUserName())){
        return ResponseData.fail("用户名不能为空！") ;
    }
    if (StrUtil.isBlank(userInfo.getPwd())){
        return ResponseData.fail("密码不能为空！") ;
    }
    if(StrUtil.isBlank(userInfo.getSex())){
        return ResponseData.fail("性别不能为空！");
    }
    if(StrUtil.isBlank(userInfo.getAddress())){
        return ResponseData.fail("地址不能为空！");
    }
    if(StrUtil.isBlank(userInfo.getAge())){
        return ResponseData.fail("年龄不能为空！");
    }
    if(checkUser(userInfo.getUserName())){

        int i=userMapper.enRoll(userInfo);
        if(i<=0){
            return ResponseData.fail();
        }
        return ResponseData.success();
    }
    return ResponseData.fail("用户已注册！");
}

    @Override
    public ResponseData<String> updateUser(UpdateInfo updateInfo) {
        if (StrUtil.isBlank(updateInfo.getUserName())){
            return ResponseData.fail("用户名不能为空！") ;
        }
        if (StrUtil.isBlank(updateInfo.getPwd())){
            return ResponseData.fail("密码不能为空！") ;
        }
        if (userMapper.updateUser(updateInfo)){
            return ResponseData.success();
        }
        return ResponseData.fail("数据修改失败,请检查用户名或密码是否正确！");
    }

    @Override
    public ResponseData<PageData<UserInfo>> page(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getPageStart(),userQuery.getPageSize());
        //存放查询的信息
        PageInfo<UserInfo> pageInfo=new PageInfo<>(userMapper.pageQuery(userQuery));
        //存放查询到的数据
        PageData<UserInfo> pageData=new PageData<>(pageInfo.getTotal(),pageInfo.getList());
        return ResponseData.success(pageData);

    }

    @Override
    public boolean checkUser(String userName) {
        UserInfo userInfo=userMapper.checkUser(userName);
       return  ObjectUtil.isEmpty(userInfo);
    }
}


//@Override
//    public ResponseData<Integer> demo(UserInfo userInfo) {
//
//        if (StrUtil.isBlank(userInfo.getUserName())){
//           return ResponseData.fail("用户名不能为空！") ;
//
//
//       }
//        if (StrUtil.isBlank(userInfo.getPwd())){
//            return ResponseData.fail("密码不能为空！") ;
//        }
//       return ResponseData.success(CollUtil.isEmpty(userInfo.getUserChildList())?0:userInfo.getUserChildList().size());
//}